package game.source.gui;

import java.awt.Point;

/*PositionControl:
 * Faz o deslocamento das naves serem contínuo e com uma determinada velocidade * 
 */
public class PositionControl{
	private double	acumulatorx;
	private double	acumulatory;
	private double	velx;			//in px/s
	private double	vely;			//in px/s
	private Point	nextPosition;
	private Point	position;

	public PositionControl(double velx, double vely, Point position){
		this.velx = velx;
		this.vely = vely;
		this.position = (Point)position.clone();
		this.nextPosition = (Point)position.clone();
		this.acumulatorx = 0.0;
		this.acumulatory = 0.0;

	}
	//	public double getVelocity(){
	//		return this.velocity;
	//	}
	public Point getPosition(){
		return (Point)this.position.clone();
	}
	public void setPosition(Point position){
		this.position.x = position.x;
		this.position.y = position.y;
	}
	public Point getNextPosition(){
		return (Point)this.nextPosition.clone();
	}
	public void setNextPosition(Point nextPosition){
		this.nextPosition.x = nextPosition.x;
		this.nextPosition.y = nextPosition.y;
	}
	public void update(long time){
		this.acumulatorx += this.velx*((double)time/1000);
		this.acumulatory += this.vely*((double)time/1000);
		if(this.acumulatorx >= 1){
			if(nextPosition.x != position.x){
				if(nextPosition.x > position.x){
					position.x += (int)this.acumulatorx;
					if(nextPosition.x < position.x)
						position.x = nextPosition.x;
				}else{
					position.x -= (int)this.acumulatorx;
					if(nextPosition.x > position.x)
						position.x = nextPosition.x;
				}
			}
			this.acumulatorx -= (int)this.acumulatorx;
		}
		if(this.acumulatory >= 1){
			if(nextPosition.y != position.y){
				if(nextPosition.y > position.y){
					position.y += (int)this.acumulatory;
					if(nextPosition.y < position.y)
						position.y = nextPosition.y;
				}else{
					position.y -= (int)this.acumulatory;
					if(nextPosition.y > position.y)
						position.y = nextPosition.y;
				}
			}
			this.acumulatory -= (int)this.acumulatory;
		}
		if(nextPosition.equals(position)){
			this.acumulatorx = 0.0;
			this.acumulatory = 0.0;
		}

	}

}
